#include "dataplot.h"
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_canvas.h>
#include <qwt_scale_widget.h>
#include <QMessageBox>
#include "peakitem.h"
//
DataPlot::DataPlot( DataFile * file, QWidget * widget ) 
	: QwtPlot( widget ), d_curve(0), d_peakCurve(0), d_data(0), d_peakData(0), d_file( file ),
	dataBegin( 0 ), dataEnd( 0 ), currentViewBegin( 0 ), currentViewEnd( 0 ), currentChannel(0)
{
	Init();
}
//
DataPlot::DataPlot( DataFile * file, const QwtText &title, QWidget * widget ) 
	: QwtPlot( title, widget ), d_curve(0), d_peakCurve(0), d_data(0), d_peakData(0), d_file( file ),
	dataBegin( 0 ), dataEnd( 0 ), currentViewBegin( 0 ), currentViewEnd( 0 ), currentChannel(0)
{
	Init();
}
//
DataPlot::~DataPlot()
{
	if( d_data ) {
		if( d_data->xData() )
			delete[] d_data->xData();
		if( d_data->yData() )
			delete[] d_data->yData();
		delete d_data;
	}
	if( d_peakData ) {
		if( d_peakData->xData() )
			delete[] d_peakData->xData();
		if( d_peakData->yData() )
			delete[] d_peakData->yData();
		delete d_peakData;
	}
	qDebug(">> DataPlot::~DataPlot();");
}

/*
void DataPlot::paintEvent(QPaintEvent * event)
{
	qDebug(">> DataPlot::paintEvent();");
}
*/

void DataPlot::Init()
{
	d_curve = new QwtPlotCurve( "c1" );
	d_peakCurve = new PeakItem( "c2" );
	d_curve->attach( this );
	d_peakCurve->attach( this );
	d_curve->setData( QwtCPointerData(0,0,0) );
	d_peakCurve->setData( CPointerPeakData(0,0,0) );
	zoomFactor = 1;
	
	d_curve->setPaintAttribute( QwtPlotCurve::PaintFiltered );
	d_curve->setPaintAttribute( QwtPlotCurve::ClipPolygons );
//	d_curve->setRenderHint( QwtPlotItem::RenderAntialiased );

//	int viewWidth = plotLayout()->canvasRect().width()-4;
//	setView( currentViewBegin, viewWidth );

	setAxisScale( QwtPlot::xBottom, 0, 1000 );
//	setAxisScale( QwtPlot::yLeft, -35000, 35000,0 );
	plotLayout()->setAlignCanvasToScales( true );
//	plotLayout()->setCanvasMargin( 15, yRight );
	axisWidget(QwtPlot::xBottom)->setMinBorderDist(0,25);

	replot();
}


void DataPlot::resizeEvent(QResizeEvent * event)
{
//	qDebug(">> DataPlot::resizeEvent();");
	QwtPlot::resizeEvent( event );
	int viewWidth = canvas()->contentsRect().width();
//	QwtScaleMap m = canvasMap(xBottom);
//	int width = m.p2() - m.p1() + 1;
//	int viewWidth = plotLayout()->canvasRect().width()-4;
	setView( currentViewBegin, viewWidth );
}

void DataPlot::panPlot(double lPos)
{
	int viewWidth = canvas()->contentsRect().width();
	setView( lPos, viewWidth );
}

void DataPlot::setView( qint64 begin, int width )
{
//	qDebug( "DataPlot::setView() > begin = %Ld", begin );
	qint64 samples = (width-1)*zoomFactor+1; // ilosc renderowanych probek (poprawione bo cos zjadlo ostatni piksel)
	qint64 allSamples = d_file->samplesCount(); // ilosc probek w pliku

	currentViewBegin = begin; // ustawienie i poprawienie początku widoku
	if( currentViewBegin < 0 )
		currentViewBegin = 0;
	currentViewEnd = currentViewBegin + samples-1; // ...oraz konca widoku
	if( currentViewEnd >= allSamples ) {
		currentViewEnd = allSamples - 1;
		// poprawka poczatku widoku jeszcze raz
		currentViewBegin = currentViewEnd - samples + 1;
		if( currentViewBegin < 0 ) { // jezeli za mało wszystkich sampli, to wykres sie rozciagnie
			currentViewBegin = 0;
			samples = currentViewEnd - currentViewBegin + 1;
		}
	}

	// zmiana rozmiaru "okna" dla danych
	if( currentViewBegin < dataBegin || currentViewEnd > dataEnd ) {
		dataBegin = currentViewBegin - samples; // do zrobienia minimalna szerokosc okna...
		if( dataBegin < 0 )
			dataBegin = 0;
		dataEnd = currentViewEnd + samples;
		if( dataEnd >= allSamples )
			dataEnd = allSamples - 1;

		// przeladowanie danych z pliku
		qint64 dataSamples = dataEnd - dataBegin + 1;
		if( zoomFactor < d_file->getBlockSize() ) {
			d_curve->show();
			d_peakCurve->hide();
			// zapamietanie starego bufora
			QwtCPointerData * old_data = d_data;
			d_data = NULL;
			// wczytanie danych do nowego bufora
			if( ( d_file->readSamples( d_data, dataBegin, dataSamples, currentChannel ) ) != -1 ) {
				// podmiana bufora
				d_curve->setData( *d_data );
			}
			// usuniecie starego bufora
			if( old_data ) {
				delete[] old_data->xData();
				delete[] old_data->yData();
				delete old_data;
			}
		} else {
			d_curve->hide();
			d_peakCurve->show();
			// zapamietanie starego bufora
			CPointerPeakData * old_peakData = d_peakData;
			d_peakData = NULL;
			// wczytanie danych do nowego bufora
			if( ( d_file->readPeaks( d_peakData, dataBegin, dataSamples, currentChannel, zoomFactor ) ) != -1 ) {
				// podmiana bufora
				d_peakCurve->setData( *d_peakData );
			}
			// usuniecie starego bufora
			if( old_peakData ) {
				delete[] old_peakData->xData();
				delete[] old_peakData->yData();
				delete old_peakData;
			}
		}
	}

	setAxisScale( QwtPlot::xBottom, currentViewBegin, currentViewEnd );
	replot();
}


void DataPlot::zoomIn()
{
	if( zoomFactor > 1/64 )
		zoomFactor /= 2;
	int viewWidth = canvas()->contentsRect().width();
	setView( currentViewBegin, viewWidth );
}


void DataPlot::zoomOut()
{
	zoomFactor *= 2;
	int viewWidth = canvas()->contentsRect().width();
	setView( currentViewBegin, viewWidth );
}

